package cn.roadtrip.mtravelbusinessmodule.bs.tuorgroup;

import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelgroup.tables.daos.BsTravelgroupStartfromcityDao;
import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelgroup.tables.pojos.BsTravelgroupStartfromcity;
import cn.roadtrip.mtravelbusinessmodule.sql.couple.travelgroup.tables.records.BsTravelgroupStartfromcityRecord;
import cn.roadtrip.utilmodule.util.DataBean;
import com.github.yitter.idgen.YitIdHelper;
import lombok.*;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.springframework.stereotype.Component;

import static cn.roadtrip.mtravelbusinessmodule.sql.couple.travelgroup.tables.BsTravelgroupStartfromcity.BS_TRAVELGROUP_STARTFROMCITY;

/**
 * 出发城市操作
 */
@Component
public class StartFromCityImpl {
    private DSLContext dslContext;
    private BsTravelgroupStartfromcityDao startfromcityDao;

    public StartFromCityImpl(
            DSLContext dslContext
    ) {
        this.dslContext = dslContext;
        this.startfromcityDao = new BsTravelgroupStartfromcityDao(dslContext.configuration());
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_getStartFromCityByTravelGroupId {
        private String traveGroupId;
    }

    /**
     * 获取出发城市
     *
     * @param model
     * @return
     */
    public DataBean getStartFromCityByTravelGroupId(Model_getStartFromCityByTravelGroupId model) {
        BsTravelgroupStartfromcity city = dslContext.selectFrom(
                        BS_TRAVELGROUP_STARTFROMCITY
                ).where(
                        BS_TRAVELGROUP_STARTFROMCITY.TRASH.eq(false)
                ).and(
                        BS_TRAVELGROUP_STARTFROMCITY.TRAVELGROUPID.eq(model.getTraveGroupId())
                )
                .limit(1).fetchOneInto(BsTravelgroupStartfromcity.class);
        if (city == null) {
            return DataBean.errEmpty();
        }
        return DataBean.suc(city, 1, "suc", "操作成功");
    }


    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_createStartFromCity {
        private String traveGroupId;
        private String cityId;
        private String cityName;
    }

    /**
     * 操作触发城市
     *
     * @param model
     * @return
     */
    public DataBean modifyStartFromCity(Model_createStartFromCity model) {
        boolean exist = startfromcityDao.exists(new BsTravelgroupStartfromcity(
                null,
                false,
                model.getTraveGroupId(),
                null,
                null
        ));
        if (exist) {
            //修改
            BsTravelgroupStartfromcityRecord r = dslContext.selectFrom(
                            BS_TRAVELGROUP_STARTFROMCITY
                    ).where(
                            BS_TRAVELGROUP_STARTFROMCITY.TRASH.eq(false)
                    ).and(
                            BS_TRAVELGROUP_STARTFROMCITY.TRAVELGROUPID.eq(model.getTraveGroupId())
                    )
                    .and(
                            model.getCityId() != null ? DSL.noCondition() : BS_TRAVELGROUP_STARTFROMCITY.CITYID.eq(model.getCityId())
                    ).and(
                            model.getCityId() != null ? DSL.noCondition() : BS_TRAVELGROUP_STARTFROMCITY.CITYNAME.eq(model.getCityName())
                    ).fetchOne();
            if (r == null) {
                return DataBean.err("city-err", "城市不存在");
            }
            r.setCityid(model.getCityId());
            r.setCityname(model.getCityName());
            int count = dslContext.executeUpdate(r);
            if (count > 0) {
                return DataBean.suc(true, 1, "suc", "操作成功");
            }
            return DataBean.err("trans-err", "修改失败");
        }
        String id = "" + YitIdHelper.nextId();
        startfromcityDao.insert(new BsTravelgroupStartfromcity(
                id,
                false,
                model.getTraveGroupId(),
                model.getCityId(),
                model.getCityName()
        ));
        return DataBean.suc(id, 1, "suc", "操作成功");
    }
}
